<template>
  <a-card :bordered="false">
    <!-- 快捷查询 -->
    <div class="c-container">
      <a-row>
        <a-col :xl="8" :lg="8" :md="8" :sm="24">
          <a-menu v-model="current" mode="horizontal" @select="selectType">
            <a-menu-item key="leads">
              <img src="@/assets/crm/leads.png" />
              <span class="title actived">线索</span>
            </a-menu-item>
            <a-menu-item key="seas">
              <img src="@/assets/crm/seas_not.png" />
              <span class="title">公海</span>
            </a-menu-item>
          </a-menu>
        </a-col>

        <a-col :xl="12" :lg="10" :md="10" :sm="24">
          <div class="searchBox">
            <a-input-search placeholder="请输入线索名称" v-model="queryParam.leadsName"  enter-button="搜索" size="default" @keyup.enter.native="searchQuery" @search="searchQuery" />
          </div>
        </a-col>
        <a-col :xl="2" :lg="4" :md="4" :sm="24">
          <a-button @click="handleAdd" type="primary" class="xr-btn--orange" icon="plus">新增</a-button>
        </a-col>
        <!-- <a-col :xl="2" :lg="4" :md="4" :sm="24">
          <a-button @click="checkShow" type="default">查重</a-button>
        </a-col> -->
      </a-row>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-row>
        <a-col :span="24">
          <oufu-scene-slider ref="oufuSlider" :sceneType="sceneType" :columns="columns"></oufu-scene-slider>
          
          <a-upload
            name="file"
            :showUploadList="false"
            :multiple="false"
            :headers="tokenHeader"
            :action="importExcelUrl"
            @change="handleImportExcel"
          >
            <a-button type="primary" icon="import">导入</a-button>
          </a-upload>
          <a-dropdown v-if="selectedRowKeys.length > 0">
            <a-menu slot="overlay">
              <a-menu-item key="1" @click="handleExportXls('线索表')"><a-icon type="delete" />导出</a-menu-item>
              <a-menu-item key="2" @click="batchDel"><a-icon type="delete" />删除</a-menu-item>
            </a-menu>
            <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button> </a-dropdown
        ></a-col>
      </a-row>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
        <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
        >项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :scroll="{ x: 1300 }"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
        <template slot="leadsName" slot-scope="text, record">
          <a-tooltip placement="top">
            <template slot="title">
              <span>{{ record.leadsName }}</span>
            </template>
            <span class="ellipsis"
              ><a @click="() => handleViewDrawer(record)">{{ record.leadsName }}</a></span
            >
          </a-tooltip>
        </template>
        <template slot="isTransform" slot-scope="isTransform">
          <div class="editable-row-operations">
            <span>
              {{ isTransform == 1 ? '已转化' : '未转换' }}
            </span>
          </div>
        </template>
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">无图片</span>
          <img
            v-else
            :src="getImgView(text)"
            height="25px"
            alt=""
            style="max-width: 80px; font-size: 12px; font-style: italic"
          />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">无文件</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="uploadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a-tooltip placement="top">
            <template slot="title">
              <span v-if="record.star == 0">添加关注</span>
              <span v-else>取消关注</span>
            </template>
            <i
              @click="toggleStar(record)"
              :style="
                record.star == 0 ? { color: '#d9d9d9', cursor: 'pointer' } : { color: '#fac23d', cursor: 'pointer' }
              "
              class="wk wk-focus-on"
            ></i>
          </a-tooltip>
        </span>
      </a-table>
    </div>

    <crmLeads-modal ref="modalForm" @ok="modalFormOk" @loadDetailInfo="handleViewDrawer" ></crmLeads-modal>
    <crmLeads-modal-drawer
      ref="modalFormDrawer"
      @handleDelete="handleDelete"
      @handleEdit="handleEdit"
    ></crmLeads-modal-drawer>
    <duplicate-check ref="checkModel"  ></duplicate-check>
  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import CrmLeadsModal from './modules/CrmLeadsModal'
import CrmLeadsModalDrawer from './modules/CrmLeadsModal__Style#Drawer'
import { httpAction, getAction } from '@/api/manage'
import { mapGetters } from 'vuex'
import { ajaxGetDictItems } from '@/api/api'

import OufuSceneSlider from '@/components/jeecg/OufuSceneSlider.vue'
import DuplicateCheck from './components/DuplicateCheck.vue'
export default {
  name: 'LeadsList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    CrmLeadsModal,
    CrmLeadsModalDrawer,
    OufuSceneSlider,
    DuplicateCheck
  },
  props: {
    typeKey: Array
  },
  data() {
    return {
      isSeas: false,
      description: '线索表管理页面',
      queryParam: {
        ownerUserId: this.userInfo().userLoginId
      },
      fieldList: [],
      superQueryFlag: false,
      superQueryParams: '',
      visible: false,
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          fixed: 'left',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          },
        },
        {
          title: '线索名称',
          width: 120,
          align: 'center',
          sorter: (a, b) => a.leadsName - b.leadsName,
          dataIndex: 'leadsName',
          fixed: 'left',
          scopedSlots: { customRender: 'leadsName' },
          ellipsis: true,
        },
        {
          title: '线索来源',
          width: 100,
          align: 'center',
          dataIndex: 'source_dictText',
          sorter: (a, b) => a.source_dictText - b.source_dictText,
          fixed: 'left',
          scopedSlots: { customRender: 'source_dictText' },
          ellipsis: true,
        },
        {
          title: '邮箱',
          width: 100,
          align: 'center',
          sorter: (a, b) => a.email - b.email,
          dataIndex: 'email',
          ellipsis: true,
        },
        // {
        //   title: '线索来源',
        //   width: 100,
        //   align: 'center',
        //   sorter: (a, b) => a.source - b.source,
        //   dataIndex: 'source',
        //   ellipsis: true,
        // },
        {
          title: '手机',
          width: 100,
          align: 'center',
          sorter: (a, b) => a.mobile - b.mobile,
          dataIndex: 'mobile',
          ellipsis: true,
        },
        {
          title: '电话',
          width: 100,
          align: 'center',
          sorter: (a, b) => a.telephone - b.telephone,
          dataIndex: 'telephone',
          ellipsis: true,
        },
        {
          title: '地址',
          width: 100,
          align: 'center',
          sorter: (a, b) => a.address - b.address,
          dataIndex: 'address',
          ellipsis: true,
        },
        {
          title: '客户行业',
          width: 100,
          align: 'center',
          dataIndex: 'industry_dictText',
          sorter: (a, b) => a.industry_dictText - b.industry_dictText,
          ellipsis: true,
        },
        {
          title: '客户级别',
          width: 100,
          align: 'center',
          dataIndex: 'level_dictText',
          sorter: (a, b) => a.level_dictText - b.level_dictText,
          ellipsis: true,
        },
        {
          title: '下次联系时间',
          width: 150,
          align: 'center',
          sorter: (a, b) => a.nextTime - b.nextTime,
          ellipsis: true,
          dataIndex: 'nextTime',
          type: 'date',
        },
        {
          title: '备注',
          width: 100,
          align: 'center',
          sorter: (a, b) => a.remark - b.remark,
          dataIndex: 'remark',
          ellipsis: true,
        },
        {
          title: '最后跟进时间',
          width: 150,
          align: 'center',
          sorter: (a, b) => a.lastTime - b.lastTime,
          dataIndex: 'lastTime',
          type: 'date',
          ellipsis: true,
        },
        {
          title: '最后一条跟进记录',
          width: 100,
          align: 'center',
          sorter: (a, b) => a.lastContent - b.lastContent,
          dataIndex: 'lastContent',
          ellipsis: true,
        },
        {
          title: '客户经理',
          width: 100,
          align: 'center',
          sorter: (a, b) => a.ownerUserId_dictText - b.ownerUserId_dictText,
          dataIndex: 'ownerUserId_dictText',
          ellipsis: true,
        },
        {
          title: '更新时间',
          width: 150,
          align: 'center',
          sorter: (a, b) => a.updateTime - b.updateTime,
          dataIndex: 'updateTime',
          ellipsis: true,
          type: 'date',
        },
        {
          title: '创建时间',
          width: 150,
          align: 'center',
          sorter: (a, b) => a.createTime - b.createTime,
          dataIndex: 'createTime',
          type: 'date',
          ellipsis: true,
        },
        {
          title: '创建人',
          width: 100,
          align: 'center',
          sorter: (a, b) => a.createUserId_dictText - b.createUserId_dictText,
          dataIndex: 'createUserId_dictText',
          ellipsis: true,
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          scopedSlots: { customRender: 'action' },
          fixed: 'right',
        },
      ],
      url: {
        list: '/online/api/pagingCrmLeads',
        delete: '/online/api/deleteCrmLeads',
        deleteBatch: '/online/api/deleteCrmLeadsBatch',
        exportXlsUrl: '/online/api/exportCrmLeadsXls',
        importExcelUrl: '/online/api/importCrmLeadsExcel',
        sceneList: '/online/api/pagingCrmScene',
        createCrmStar: '/online/api/createCrmStar',
      },
      dictOptions: {},
      placeholder: '请输入客户名称/手机/电话',
      inputContent: '',
      current: ['leads'],
      //场景定义开始
      sceneType: 'CmsLeads',
      //场景定义结束
    }
  },
  watch:{
    typeKey(val){
      this.current = val
    }
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  methods: {
    ...mapGetters(['userInfo']),
    initDictConfig() {
      const that = this
      this.$bus.on('superParam', function (param) {
        console.log(param)
        if (param.type == 1) {
          that.superQueryParams = ''
          that.superQueryFlag = false
          if (param.data != 0) {
            that.queryParam.sceneId = param.data
            that.loadData()
          } else {
            delete that.queryParam.sceneId
            that.loadData()
          }
        }
        if (param.type == 2) {
          //高级查询方法
          if (!param.data) {
            that.superQueryParams = ''
            that.superQueryFlag = false
          } else {
            that.superQueryFlag = true
            that.superQueryParams = JSON.stringify(param.data)
          }
          that.loadData()
        }
      })
    },
    // 切换线索公海事件
    selectType(e) {
      this.$emit("menu-select", e);
    },
    toggleStar(data) {
      this.loading = true
      getAction(this.url.createCrmStar, { id: data.id, crmType: 'leads' })
        .then(() => {
          this.loading = false
          this.$message.success(data.star > 0 ? '取消关注成功' : '关注成功')
          data.star = data.star > 0 ? 0 : 1
        })
        .catch(() => {
          this.loading = false
        })
    },
    checkShow(){
      this.$refs.checkModel.show()
    },
    reloadDrawerData(id){
      this.$refs.modalFormDrawer.getData(id)
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
@import '~@assets/less/pageHead.css';
</style>
<style lang="less" scoped>
.tips {
  display: inline-block;
  position: absolute;
  left: 30px;
  width: 20px;
  height: 20px;
  line-height: 20px;
  border-radius: 50%;
  text-align: center;
  font-size: 14px;
  background-color: #168a40;
  color: #fff;
}
</style>
